<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#	
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
# check_availability.php:
#
#			Checks availability of a particular "to" url. 
#			Called through ajax requests by add_url.php .
##############################################################################################

require_once("functions.inc.php");

// First things first
prevent_mysql_injection();

$from = $_POST['from'];
$to = $_POST['to'];

// No from and to :(
if ( $from == "" && $to == "" ) 
	exit;

//echo '<img id="progress" style="display:none" src="compress_url_libs/progress.gif">';

// Validate the to url
$validate = preg_match("/[a-z0-9_-]+/", $to, $array);

// 0th element should be same as "to" if regex was matched fully
if($array[0] != $to ){
   //error message
   echo "<span class='cant_compress'>
	 					\"To\" URL can contain a-z, 0-9 or _ chars only
				</span>";
   exit;
}

// Add "http://" if not already present
if( strpos ( $from, "http://" ) === false )
	$from = "http://".$from;

// find the path like http://localhost/ instead of http://localhost/url_compressor_libs/
$path = get_outer_path();

// Compress
if(isset($_POST['compress'])) {

	// Check if "to" is already taken
	$r = query ( "select * from `" . $MYSQL_TABLE . "` where `to`='". $to ."'" );
	
	if($a = mysql_fetch_array($r)) {

		echo "<span class='cant_compress'>
						Cannot compress to <a target='_blank' href='".$path.$a['to']."'>".
								$path.$a['to']."</a> as it is already taken
					</span>";
	}
	else {
		// "to" is available
		query( "INSERT INTO `". $MYSQL_TABLE ."` ( `id` , `to` , `from` ) VALUES ('', '". $to ."', '". $from ."');");

		echo "<span class='compressed'>
						Compressed from <a target='_blank' href='". $from ."'>". $from ."</a> to 
						<a target='_blank' href='".$path . $to."'>".$path . $to."</a>
					</span>";
	}
	exit;
}

// Check availability
$r = query("select * from `". $MYSQL_TABLE ."` where `from`='". $from ."' limit 1");
if(mysql_num_rows($r) > 0) {
	$a = mysql_fetch_array($r);
	echo "<span class='exists'>Already exists : <a target='_blank' href='".$path.$a['to']."'>".$path.$a['to']."</a></span>";
}
if($_POST['to'] != ""){
	$r=query("select * from `".$MYSQL_TABLE."` where `to`='".$_POST['to']."' limit 1");
	if(mysql_num_rows($r) > 0) {
		echo "<span class='not_available'>Not Available</span>";
	}
	else {
		echo "<span class='available'>Available</span>";
	}
}
if ( $DEBUG ) print_r($_POST);
?>